package com.virtium.vpm.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

/**
 * Provides String utility methods that can be used to perform common operations.
 * 
 * @author TuanAnh
 *
 */
public class StringUtil {
	
	private static Logger log = Logger.getLogger(StringUtil.class);

	/**
	 * 
	 */
	public StringUtil() {
	}
	
    /**
     * Returns false if the string value is not null or empty, true otherwise.
     *
     * @param fieldValue value to be validated
     * @return
     */
    public static boolean isEmpty(String fieldValue) {

        if ((fieldValue == null) ||
                (fieldValue == "") ||
                (fieldValue.equalsIgnoreCase(""))) {
            return true;
        }

        return false;
    }	

    /**
     * Converts normal Array of <code>String</code> into <code>List</code>.
     *
     * @param arrStr Array of String to be converted
     * @return a List
     */
    public static List toList(String[] arrStr) {

        List ret = null;
        try {
            if (arrStr != null) {
                ret = Arrays.asList(arrStr);
            }
        } catch (Exception e) {
            log.debug("Exception toList", e);
        } finally {
            return ret;
        }
    }    
	
    /**
     * This method returns a Collection of String objects without the delimiters.
     *
     * @param theString The string to be formatted
     * @param delim     The delimiter
     * @return Collection
     */
    public static Collection<String> removeDelimiter(String theString, String delim) {
        Collection<String> stringCol = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(theString, delim);
        while (st.hasMoreTokens()) {
            stringCol.add(st.nextToken().trim());
        }
        return stringCol;
    }    
}
